import { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import type { RootState } from '../redux/store';
import { changeCityList } from '../redux/features/citySlice';
import { findCities } from '@/api/MaiZuo';

const useCityList = () => {
  const list = useSelector((state: RootState) => state.city.list);
  const dispatch = useDispatch();

  useEffect(() => {
    if (list.length === 0) {
      findCities().then((res: any) => {
        console.log('city findCities', res);
        dispatch(changeCityList(res.cities));
      });
    } else {
      console.log('city 缓存');
    }
    return () => {};
  }, [list.length, dispatch]);
  return { list };
};

export { useCityList };
